package com.tbit.main.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tbit.main.constant.GatewayProtocol;
import com.tbit.main.constant.HelmetStatus;
import com.tbit.main.pojo.Helmet;
import com.tbit.main.pojo.HelmetAbnormal;
import com.tbit.main.pojo.HelmetDataVol;
import com.tbit.main.pojo.Machine;
import com.tbit.main.service.HelmetAbnormalService;
import com.tbit.main.service.HelmetBoundService;
import com.tbit.main.service.HelmetService;
import com.tbit.main.service.MachineService;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.LogUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 14:09 2023/3/8
 */
@Component
@Slf4j
public class HelmetEventListener {

    @Autowired
    private HelmetAbnormalService helmetAbnormalService;
    @Autowired
    private HelmetService helmetService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private HelmetBoundService helmetBoundService;

    @RabbitListener(queues = {"${mq.queue.tbitHelmetEvent}"}, concurrency = "5")
    @RabbitHandler
    public void onMessage(Message msg) {
        try {
            String respMsg = new String(msg.getBody(), "UTF-8");
            /**判断消息类型*/
            JSONObject object = JSON.parseObject(respMsg);
            LogUtil.info("收到mq推送消息" + object.toJSONString());
            int msgId = object.getIntValue("msgId");
            /**{"feedback":"","data":{"mno":"009480402","type":3,"vol":0.0},"msgId":40015}*/
            switch (msgId) {
                case GatewayProtocol.MQ_MSG_ID_EVENT_HELMET_VO_PUSH:
                    HelmetDataVol helmetDataVol = JSON.parseObject(object.getString("data"), HelmetDataVol.class);
                    Machine machine = machineService.getByMachineNO(helmetDataVol.getMno());
                    if (machine == null) {
                        break;
                    }
                    Integer type = HelmetStatus.HELMET_SOC;
                    HelmetAbnormal helmetAbnormal = helmetAbnormalService.getMachineIdType(machine.getMachineId(), type);
                    if (helmetDataVol.getVol() != null && helmetDataVol.getVol() < 2.5) {
                        Helmet helmet = helmetBoundService.getMachineId(machine.getMachineId());
                        if (helmetAbnormal == null) {
                            LogUtil.info("添加头盔锁异常");
                            helmetAbnormalService.insert(new HelmetAbnormal(null, machine.getMachineId(), null, null, type,
                                    DateTimeUtil.getNowTime(), null, null));
                            helmetService.updateState(helmet.getHelmetMac(), 1);
                        }
                    } else {
                        if (helmetAbnormal != null) {
                            helmetAbnormalService.del(machine.getMachineId(), HelmetStatus.HELMET_SOC);
                        }
                    }
            }
        } catch (Exception e) {
            log.error("[HelmetEventListener] [onMessage] {}", e.getMessage(), e);
        }
    }
}
